package com.yc.store.servlet;

import com.google.gson.Gson;
import com.yc.store.bean.Result;
import com.yc.store.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginInfo")
public class LoginInfoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession(false).getAttribute("user");
        Result result;
        if(user == null){
            result = new Result(404,"用户还没有登录",null);
        }else{
            result = new Result(200,"已查到用户信息",user);
        }
        resp.setContentType("application/json;charset=utf-8");
        Gson gson = new Gson();
        String json = gson.toJson(result);
        resp.getWriter().append(json);
    }
}
